﻿using System.Collections.Generic;
using System.Text;
using SmartContentModule.Helpers;

namespace SmartContentModule.CssModel
{
    internal class CssFileInfo
    {
        private string contentUrl;
        private string directory;
        private string tempPath;
        private string filePath;

        /// <summary>
        /// Constructor for CssFileInfo
        /// </summary>
        /// <param name="contentUrl">relative path for css file</param>
        /// <param name="directory">directory of css file</param>
        internal CssFileInfo(string contentUrl, string directory)
        {
            this.contentUrl = contentUrl;
            this.directory = directory;
            this.filePath = IOHelper.GetFilePath(contentUrl, directory);
            Update();
        }

        /// <summary>
        /// Get current version for this CssFileInfo. Building of hash is simply concatting last update time
        /// of dependent file and all image files contained in css file (CssUrlInfo list for CssFileInfo)
        /// </summary>
        /// <returns>Returns Version</returns>
        internal string GetVersion()
        {
            StringBuilder builder = new StringBuilder();

            builder.Append(LastModification);
            if (UrlInfoList != null)
            {
                foreach (CssUrlInfo info in UrlInfoList)
                {
                    builder.Append(info.LastModification);
                }
            }

            string urlHash = ContentUrl.GetHashCode().ToString();
            string lastModificationHash = builder.ToString().GetHashCode().ToString();
            return (urlHash + lastModificationHash).GetHashCode().ToString();
        }

        /// <summary>
        /// Updates temp file, this is used to call when original file has any changes or any dependency such as image files has changed
        /// </summary>
        internal void Update()
        {
            RefreshUrlList();
            CreateTempFile();
        }

        /// <summary>
        /// Gets or sets UrlInfoList
        /// </summary>
        internal List<CssUrlInfo> UrlInfoList
        {
            get;
            set;
        }

        /// <summary>
        /// Content url, i.e ~/Content/css/general.css
        /// </summary>
        internal string ContentUrl
        {
            get { return contentUrl; }
        }

        /// <summary>
        /// File path
        /// </summary>
        internal string FilePath
        {
            get { return filePath; }
        }

        /// <summary>
        /// Temp path for file, this contains a modified version of original content
        /// </summary>
        internal string TempPath
        {
            get { return tempPath; }
        }

        /// <summary>
        /// String representation of last modified time in format yyyyMMddHHmmss
        /// </summary>
        internal string LastModification
        {
            get { return IOHelper.GetLastModificationPostfix(FilePath); }
        }

        private void RefreshUrlList()
        {
            this.UrlInfoList = CssAnalyser.GetCssUrlInfoList(filePath, directory);
        }

        private void CreateTempFile()
        {
            string modifiedContent = IOHelper.ReadFile(FilePath);
            if (IOHelper.IsCssFile(FilePath))
                modifiedContent = CssAnalyser.GetModifiedCss(modifiedContent, UrlInfoList);

            string newTempPath = IOHelper.GetTempFilePath();
            IOHelper.WriteFile(modifiedContent, newTempPath);
            this.tempPath = newTempPath;
        }
    }
}
